package com.technology.education.ui.teacher.popularize

import androidx.core.os.bundleOf
import com.angcyo.dsladapter.dpi
import com.angcyo.dsladapter.dslItem
import com.angcyo.dsladapter.toDslItemList
import com.angcyo.dsladapter.updateOrInsertHeaderItem
import com.google.android.material.imageview.ShapeableImageView
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.model.InviteList
import com.technology.education.dslitem.ItemSingleText
import com.technology.education.ext.getColor
import com.technology.education.ext.header
import com.technology.education.ext.navigation
import com.technology.education.ext.setPageItems
import me.hgj.jetpackmvvm.ext.parseState

/**
 *@author:create by BZY
 *Date: 2022/3/2 07
 * 我的推广-我邀请的辅导员
 */
class PopularizeRecordFragment : BaseRecyclerTopBarFragment<PopularizeRecordViewModel>() {
    override fun initViews() {
        setTitle("我的推广")
        recycler.setBackgroundColor(getColor(R.color.bg_activity_gray))
    }

    override fun lazyLoadData() {
        loading()
        setUI()
    }

    private fun setUI() {
        renderAdapter {
            updateOrInsertHeaderItem<ItemSingleText>("title", 0) {
                it.apply {
                    style.topPadding = 24
                    style.isBold = true
                    style.itemBackColor = R.color.bg_activity_gray
                    title = "我邀请的辅导员"
                }
            }

        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { it ->
                hasMore(it.total)
                renderAdapter {
                    setPageItems(it.list.toDslItemList(R.layout.item_popularize_record) {
                        itemBottomInsert = 12 * dpi
                        itemDecorationColor = R.color.bg_activity_gray
                        itemBindOverride = { itemHolder, _, _, _ ->
                            itemData?.let { it ->
                                if (it is InviteList) {
                                    itemHolder.tv(R.id.tv_name)?.text = it.name
                                    itemHolder.tv(R.id.tv_info)?.text =
                                        "辅导总时长: ${it.durationString}"
                                    itemHolder.v<ShapeableImageView>(R.id.iv_header)?.apply {
                                        header(it.avatar, this)
                                    }
                                    itemHolder.clickItem { view ->
                                        navigation(
                                            view,
                                            R.id.action_popularize_record_to_detail,
                                            bundleOf("info" to it)
                                        )
                                    }
                                }
                            }
                        }
                    }, mViewModel.pageIndex)
                }
            }, onError = { loadError() })
        }
    }
}